package financial.sharing.center.system.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.EnableStatusEnum;
import financial.sharing.center.common.enums.ResponseContext;
import financial.sharing.center.common.vo.Token;
import financial.sharing.center.system.dao.SysAdminMapper;
import financial.sharing.center.system.model.entity.SysAdmin;
import financial.sharing.center.system.service.SysAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName SysAdminServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/4/22
 * @Version 1.0
 */
@Service
public class SysAdminServiceImpl implements SysAdminService {

    @Autowired
    private SysAdminMapper sysAdminMapper;

    @Override
    public Token getToken(String username, String password) {
        SysAdmin sysAdmin = this.getAdmin(username);
        if (sysAdmin == null || !sysAdmin.getUserPassword().equals(password)){
            return new Token(-1, "admin", username);
        }
        return new Token(sysAdmin.getId(), "admin", sysAdmin.getUserName());
    }

    private SysAdmin getAdmin(String username){
        Example example = new Example(SysAdmin.class);
        example.createCriteria().andEqualTo("userName", username)
                                .andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        return this.sysAdminMapper.selectOneByExample(example);
    }

    private SysAdmin getAdmin(Integer id){
        return this.sysAdminMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysAdmin> getAllSysAdmin(String selectUserName) {
        Example example = new Example(SysAdmin.class);
        example.createCriteria().andLike("userName", "%"+selectUserName+"%")
                                .andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        List<SysAdmin> sysAdminList = this.sysAdminMapper.selectByExample(example);
        return new PageInfo<>(sysAdminList);
    }

    @Override
    public Integer createOneSysAdmin(String userName, String userPassword) {
        SysAdmin sysAdmin = this.getAdmin(userName);
        if (sysAdmin != null){
            return ResponseContext.已经存在.getValue();
        }
        sysAdmin = new SysAdmin(userName, userPassword);
        int i = this.sysAdminMapper.insertSelective(sysAdmin);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.创建失败.getValue();
    }

    @Override
    public Integer compileOneSysAdmin(SysAdmin sysAdmin) {
//        if (this.getAdmin(sysAdmin.getUserName()) != null){
//            return ResponseContext.已经存在.getValue();
//        }
        int i = this.sysAdminMapper.updateByPrimaryKey(sysAdmin);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    @Override
    public Integer deleteOneSysAdmin(Integer id) {
        SysAdmin sysAdmin = this.getAdmin(id);
        sysAdmin.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.sysAdminMapper.updateByPrimaryKey(sysAdmin);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }
}
